(function umd(root,factory){
	if(typeof module==='object' && typeof exports === 'object' )
		module.exports=factory()
	else if(typeof define==='function' && define.amd)
		define([],factory)
	else
		root.httpVueLoader=factory()
})(this,function factory() {  
	var scopeStyles=function(styleElt, scopeName,fn){ 
		function process() { 
			var list=[];
			var sheet = styleElt.sheet;
			var rules = sheet.cssRules; 
			for ( var i = 0; i < rules.length; ++i ) { 
				var rule = rules[i];
				if ( rule.type !== 1 )
					continue; 
				var scopedSelectors = []; 
				rule.selectorText.split(/\s*,\s*/).forEach(function(sel) { 
					scopedSelectors.push(scopeName+' '+sel);
					var segments = sel.match(/([^ :]+)(.+)?/);
					scopedSelectors.push(segments[1] + scopeName + (segments[2]||''));
				});

				var scopedRule = scopedSelectors.join(',') + rule.cssText.substr(rule.selectorText.length);
				sheet.deleteRule(i);
				sheet.insertRule(scopedRule, i);
				list.push(scopedRule);
			}
			fn(list.join(""));
		}

		try {
			// firefox may fail sheet.cssRules with InvalidAccessError
			process();
		} catch (ex) {

			if ( ex instanceof DOMException && ex.code === DOMException.INVALID_ACCESS_ERR ) {

				styleElt.sheet.disabled = true;
				styleElt.addEventListener('load', function onStyleLoaded() {

					styleElt.removeEventListener('load', onStyleLoaded);

					// firefox need this timeout otherwise we have to use document.importNode(style, true)
					setTimeout(function() {

						process();
						styleElt.sheet.disabled = false;
					});
				});
				return;
			}

			throw ex;
		}
	} 
	function nextNode(els,fn){
		for(var i=0;i<els.length;i++){
			if(els[i].nodeType== Node.ELEMENT_NODE){
				fn(els[i]);
				nextNode(els[i].childNodes,fn);
			}
		}
	}
	function getRootElt(elt){
		var tplElt = elt.content || elt;
		if ( 'firstElementChild' in tplElt ){
			return tplElt.firstElementChild;
		}
		for ( tplElt = tplElt.firstChild; tplElt !== null; tplElt = tplElt.nextSibling ){
			if ( tplElt.nodeType === Node.ELEMENT_NODE )
				return tplElt;
		}
			return null;
	} 
	var toStyleNext=function(styles,index,fn){
//				console.log(str);
		if(index<styles.length){
			fn(index,styles[index]).then(function(){
				toStyleNext(styles,index+1,fn);
			});
		}else{
			fn(index,null)
		}
	}
	var toStyle=function(styles,scopeName){ 
		var str="";
		var fn=function(css){
			str+=css; 
		}
		toStyleNext(styles,0,function(index,style){
			return  new Promise(function(resolve, reject){
				if(style){
					var styleBody=style.innerHTML; 
					var lang=style.getAttribute("lang");
					if(lang=="less"){
						require(["less"],function(less){
							var parser = new(less.Parser)({
								    filename: scopeName,
							});    
							parser.parse(styleBody, function (err, css) { 
							    if (err) {
							        if (typeof console !== 'undefined' && console.error) {
							            console.error(err);
							        }
							    } else { 
							    		 
									fn(css.toCSS()); 
							    }  
							    resolve();
							});	
						});
					}else{
						fn(styleBody); 
						resolve();
					}
				}else{ 
					 styles[0].innerHTML=str;
					 scopeStyles(styles[0],scopeName,function(toCss){
					 	styles[0].innerHTML=toCss;
					 	document.head.appendChild(styles[0])
					 });
					  
					 
				 
				}
			});
		}) 
	}
	var a = document.createElement('a');
	return {
		load:function(name, req, onLoad, config,requireBody){
			var map=requireBody.map;
			var context=requireBody.context; 
			var url=require.toUrl(name); 
			var nextId=1;
			require(["requireUtil",'babel'],function(requireUtil,babel){
				requireUtil.http(url).then(function(view){ 
					var doc = document.implementation.createHTMLDocument('');
					doc.body.innerHTML = view;
					var template=null;
					var script=null;
					var styles=[];
					for ( var it = doc.body.firstChild; it; it = it.nextSibling ) {
						switch ( it.nodeName ) {
							case 'TEMPLATE':
								template = it;
								break;
							case 'SCRIPT':
								script =it;
								break;
							case 'STYLE':
								styles.push(it);
								break;
						}		
					}
					var id=nextId++;
					toStyle(styles,"[data-id-"+id+"]");  
					map.nextValue=function(dom){
						nextNode([getRootElt(template)],function(item){
							item.setAttribute("data-id-"+id, '');   
						}) 
						dom.default.template=template.innerHTML; 
						 
					} 
					var code=babel.transform(script.textContent,{
							presets:["es2015",'env'],
							plugins:["transform-async-to-generator", 'transform-modules-amd'
							]
						});   
					
					a.href = url;  
						eval(code.code
						+'\n//@ sourceURL='+a.href
						);  
					var next=context.completeLoad(map.id);
					if(next){
						map.nextValue(next); 
					}
					
				}); 
			})
			 
			
			
		}
	};
});